<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use app\common\model\User;

/**
 * 手机短信
 */
class Sms extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    /**
     * @ApiTitle    (发送验证码)
     * @ApiSummary  (描述)
     * @ApiBody     (event 登录mobileLogin 修改手机号changeMobile 重置密码resetPwd 绑定手机号bindMobile)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/sms/send)
     * @ApiParams   (name="mobile", type="string", required=true, description="手机号", sample="")
     * @ApiParams   (name="event", type="string", required=true, description="事件名称 mobileLogin=手机号登录 resetPwd=重置密码 changeMobile=修改手机号", sample="mobileLogin")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ({
    "code": 1,
    "msg": "发送成功",
    "time": "1564540603",
    "data": null
    })
     */
    public function send()
    {
        $mobile = $this->request->request("mobile");
        $event = $this->request->request("event");
        $event = $event ? $event : 'register';

        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('手机号不正确'));
        }

        $ret = $this->post('/api/sms/send', compact('mobile', 'event'));

        $this->result($ret['msg'], $ret['data'], $ret['code']);
    }

    /**
     * @ApiTitle    (检测验证码)
     * @ApiSummary  (描述)
     * @ApiBody  (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/sms/check)
     * @ApiParams   (name="mobile", type="string", required=true, description="description", sample="")
     * @ApiParams   (name="event", type="string", required=true, description="description", default="register", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ()
     */
    public function check()
    {
        $mobile = $this->request->request("mobile");
        $event = $this->request->request("event");
        $event = $event ? $event : 'register';
        $captcha = $this->request->request("captcha");

        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('手机号不正确'));
        }

        $ret = $this->post('/api/sms/send', compact('mobile', 'event', 'captcha'));

        $this->result($ret['msg'], $ret['data'], $ret['code']);
    }
}
